package wsepForum.ui.jaxcent.states;

import java.util.Collection;
import java.util.Map;
import java.util.Vector;

import jaxcent.HtmlElement;
import jaxcent.HtmlInputButton;
import jaxcent.HtmlOption;
import jaxcent.HtmlSelect;
import jaxcent.JaxcentPage;
import jaxcent.Jaxception;
import wsepForum.common.elements.Section;
import wsepForum.ui.jaxcent.IXContext;

/**
* @author Oren
*
*/
public abstract class AXSectionControl implements IXState {

	protected HtmlElement xTitle;
	protected HtmlInputButton xSubmit;
	protected HtmlSelect xModerators;
	protected IXContext context;
	protected Section section;

	/*****************************
	 * CONSTRUCTOR               *
	 *****************************/
	public AXSectionControl(IXContext context, Section section) {
		this.context = context;
		this.section = section;
		construct();
		initContent();
	}

	/*****************************
	 * MEMBERS                   *
	 *****************************/

	/* (non-Javadoc)
	 * @see wsepForum.network.server.jax.pages.states.XState#getType()
	 */
	@Override
	public StateType getType() {
		return StateType.SECTIONCONTROL;
	}

	/**
	 *
	 */
	protected void initContent() {
		final JaxcentPage page = this.context.getPage();
		this.xTitle = new HtmlElement(page,"xtitle");
		this.xModerators = new HtmlSelect(page,"xmoderators" );
		this.xSubmit = new HtmlInputButton(page, "xsectionsubmit" ) {
		        @SuppressWarnings("unchecked")
				protected void onClick(Map pageData) {
		        		try {
		        			setEnabled(false);
		        			String oldButton = getValue();
							setValue("Saving...");
	        				String sectionName = (String) pageData.get( "xsectionname" );
							HtmlOption option;
							Vector <String> newModerators = new Vector<String>();
							for (int i=0, size=xModerators.getLength();i<size;i++) {
								option = xModerators.getOption(i);
								if (option.getSelected()) {
									newModerators.add(option.getText());
								}
							}
							dealWithSubmit(sectionName, newModerators);
							setValue(oldButton);
							setEnabled(true);
						} catch (Jaxception e) {
							context.getPage().showMessageDialog("Cannot read from page input fields, try reloading the page\n"+e.getMessage());
							context.loadSection(0);
						}
		        }
		    };
	}

	/**
	 * @param sectionName
	 * @param newModerators
	 */
	protected abstract void dealWithSubmit(String sectionName, Collection<String>newModerators) throws Jaxception;

	/**
	 *
	 */
	protected abstract void construct();

	/**
	 * @param sectionField
	 * @param endControls
	 */
	protected void construct(String sectionField, String endControls) {
		this.context.setBody(
				"<form id=\"sessoioncontrol\"><div class=\"panel\">"+
				"<div class=\"inner\"><span class=\"corners-top\"><span></span></span>"+
				"<div class=\"content\"><h2 id=\"xtitle\"></h2>"+
				"<fieldset class=\"fields1\">" +
				"<dl><dt><label for=\"xsectionname\">Section name:</label></dt>" +
				"<dd>"+ sectionField + "</dd></dl>"+
				"<dl><dt><label for=\"moderators\">Add Moderators:</label><br /><span>Select users that will serve as moderators for this section.</span></dt><dd>"+
				"<select tabindex=\"2\" name=\"xmoderators[]\" id=\"xmoderators\" multiple=\"multiple\" size=\"10\"></select></dd>" +
				"</dl>" +
				endControls + "<dl><dt>&nbsp;</dt><dd>"+
				"<input type=\"button\" name=\"xsectionsubmit\" id=\"xsectionsubmit\" tabindex=\"6\" value=\"\" class=\"button1\" /></dd>"+
				"</dl></fieldset></div><span class=\"corners-bottom\"><span></span></span></div></div></form>");
	}

}